/*Copyright (C) 2009 - 2011 Careil Baptiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../headers.h"

int lier_touches() {
#ifdef WIN32
#else /* !WIN32 */
	if(
		/* kD : delete */
		define_key("\x7F", M_KEY_BACKSPACE) == ERR ||
		/* backtab */
		define_key("\033[Z", M_KEY_BACKTAB) == ERR
	) {
		return 1;
	}
#endif /* WIN32 */
	return 0;
}

void init_curses_fail(const char *str) {
	endwin();
	printf("\nERREUR !\n");
	Log(LOG_DEFAULT, str);
}

bool init_curses() {
	bool ret_val = true;


#ifndef WIN32
	/* Active l'UTF-8 */
	printf("\n Activation de l'UTF-8 : ");

	if(setlocale(LC_ALL, "") == NULL) {
		printf("!!");
		CSLP_Sleep(500);
		ret_val = false;
	}
	else {
		printf("OK");
	}

#endif /* !WIN32 */

	if(CSLP_InitCurses(CSLP_INIT_NO_ECHO | CSLP_INIT_RAW | CSLP_INIT_KEYPAD | CSLP_INIT_COLORS, 0) != 0) {
		init_curses_fail("CSLP_InitCurses : Impossible d'initialiser curses.");
		ret_val = false;
	}
	else if(CSLP_InitMainColorPairs() != 0) {
		init_curses_fail("CSLP_InitMainColorPairs : Impossible d'initialiser les couleurs.");
		ret_val = false;
	}
	else {
		CSLP_Sleep(100);
	}

	/* ne gene pas le lancement du programme en cas d'erreur */
	if(lier_touches()) {
		Log(LOG_DEFAULT, "Le liage de certaines touches a echoue.");
	}

	return ret_val;
}

